require 'sequel'

DB = Sequel.open 'sqlite:///orders.db'

class Order < Sequel::Model(:orders)
  has_many :items, :class => Item, :through => :order_items
  # source foreign key = order_id, target foreign key = item_id
  # (derived from class names)
  
  # this should construct the following query:
  # SELECT * FROM items LEFT INNER JOIN order_items ON items.id = order_items.item_id
  #   WHERE order_items.order_id = @pkey
  
  # or, in sequel-speak:
  # Item.join(:order_items, :item_id => :items__id).filter(:order_items__order_id => @pkey)
  
  def total
    items.sum(:price)
  end
end

